package com.bolt.report.engine;

import com.bolt.convention.data.Record;
import com.bolt.report.engine.fill.FillReturnValue;
import com.bolt.report.engine.model.DataRange;

public class BindData {

    private String key;

    private String fieldName;

    private DataRange range;

    private Object data;

    public BindData(Object data) {
        this.data = data;
    }

    public BindData(String key, String fieldName, Object data, DataRange range) {
        this.key = key;
        this.fieldName = fieldName;
        this.data = data;
        this.range = range;
    }


    public BindData(String key, DataRange range) {
        this.range = range;
        this.key = key;
    }

    public Object getData() {
        return data;
    }

    public Record getWrapData(FillReturnValue returnValue) {
        if (!isMultiple()) {
            return returnValue.getRawData(this.start());
        }
        return getWrapData();
    }

    public Record getWrapData() {
        if (this.getData() instanceof Record) {
            return (Record) this.data;
        } else {
            Record record = new Record();
            record.put(this.fieldName, this.data);
            return record;
        }
    }


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public int Offset() {
        return range.offset();
    }

    public int start() {
        return range.getStart();
    }

    public int end() {
        return range.getEnd();
    }

    public boolean isMultiple() {
        return range != null && (range.getEnd() - range.getStart()) > 0;
    }

    public DataRange getRange() {
        return range;
    }
}